package com.bridge.common.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.bridge.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.util.Map;


public abstract class AbstractSms {

    @Autowired
    private ApplicationContext applicationContext;
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
    private static Logger logger = LoggerFactory.getLogger(AbstractSms.class);
    //ThreadPoolTaskExecutor executor = null;
    private String toPhone;
    private Map<String, String> vars = null;

   // private ISmsLogService smsLogService;
    JSONObject jsonObject=null;

    private SmsInfo smsInfo;

    public SmsInfo getSmsInfo() {
        return smsInfo;
    }

    public void setSmsInfo(SmsInfo smsInfo) {
        this.smsInfo = smsInfo;
    }

    public abstract int  getSmsType();

    public AbstractSms() {

    }



    public abstract String getProject();//模板ID



    public JSONObject getTemplateParam() {
        if(jsonObject!=null){
            return jsonObject;
        }
        jsonObject = new JSONObject();
        jsonObject.put("code", getCode());
        return jsonObject;
    }

    public void setTemplateParam(JSONObject jsonObject) {
        this.jsonObject=jsonObject;
    }


    public String getToPhone() {
        return toPhone;
    }

    public abstract void setCode(String code);
    public abstract String getCode();


    public void setToPhone(String toPhone) {
        this.toPhone = toPhone;
    }


    public void sendSms() {
        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");


            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsInfo.getAccessKey(), smsInfo.getAccessKeySecret());
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            //必填:待发送手机号
            request.setPhoneNumbers(getToPhone());
            //必填:短信签名-可在短信控制台中找到
            request.setSignName(smsInfo.getSignName());
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode(getProject());
            //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
            if (!StringUtil.isNullOrEmpty(getTemplateParam()))
                request.setTemplateParam(JSONObject.toJSONString(getTemplateParam()));


            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);


            logger.error("短信接口:"+"phone:"+getToPhone());
            logger.info(sendSmsResponse.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("请求短信接口失败");

        }


    }


}
